<?php
class UserStatus extends AppModel {
	public $name = 'UserStatus';
	//The Associations below have been created with all possible keys, those that are not needed can be removed

	public $belongsTo = array(
		'User' => array(
			'className' => 'User',
			'foreignKey' => 'user_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);
	
	public function status( $user_id, $fieldname = 'all' ){
		$status = array();
		if($fieldname != 'all'){
			$byField =  $this->find(
				'first', 
				array(
					'conditions' => array('UserStatus.user_id' => $user_id, 'UserStatus.name_field' => $fieldname),
					'recursive' => -1,
				)
			);
			
			$status[$fieldname] = $byField['UserStatus']['value'];
			
		}else{	
			
			$statuses =  $this->find(
				'all', 
				array(
					'conditions' => array('UserStatus.user_id' => $user_id),
					'recursive' => -1,
				)
			);
			
			if(!empty($statuses)){
				foreach($statuses as $st){
					$status[$st['UserStatus']['name_field']] = $st['UserStatus']['value'];
				}
			}
		}
		
		if(!empty( $status )){
			
			return $status;
			
		}else{
			return false;
		}
		
	}

        public function updateUserStatus($userId, $field, $oparator = null, $value = null){

            if($oparator){
                $this->updateAll(
                            array( 'UserStatus.value' => 'UserStatus.value' .  $oparator . 1 ),
                            array( 'UserStatus.user_id' => $userId, 'UserStatus.name_field' => $field )
                    );
            }else{
                     $this->updateAll(
                            array( 'UserStatus.value' => $value ),
                            array( 'UserStatus.user_id' => $userId, 'UserStatus.name_field' => $field )
                    );
            }

        }
	
}
?>